home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
EXGRAF3D
/
BOXES.C
next >
Wrap
C/C++ Source or Header
|
1990-09-27
|
5KB
|
226 lines
/*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
/* */
/* Boxes.c -- Translation of Pascal 'LISA/EXAMPLE/BOXES.TEXT' */
/* from Apple Programmer's and Developer's Association */
/* disk 'Macintosh Example Applications and Sources v.1.0' */
/* */
/* Translation to LightSpeed C by Lewis E. Garrett - 9/27/90 */
/* CIS 71147,2202 */
/* */
/* */
/*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
#include "Graf3D.h"
/* CONST */
#define boxCount 15
#define keyOrMouse mDownMask+keyDownMask
#define NIL 0L
typedef struct Box3D{
Point3D pt1;
Point3D pt2;
};
WindowPtr w;
/* GrafPort myPort; */
Port3D gPort3D;
struct Box3D boxArray[15];
int nBoxes;
int i;
EventRecord dummy;
Port3D *ptr3D;
main()
{ /* main program */
FlushEvents(everyEvent, 0);
InitGraf(&thePort);
ptr3D = &gPort3D;
InitGrf3D(&ptr3D);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(NIL);
InitCursor();
HideCursor();
w = NewWindow(NIL, &screenBits.bounds, "\p", TRUE, 2, (WindowPtr)(-1L), FALSE, 0L);
SetPort(w);
Draw3D();
DisposeWindow(w);
}
Draw3D()
{
Rect inRect;
long n;
Open3DPort(&gPort3D);
ViewPort(&thePort->portRect); /* put the image in this rect */
LookAt(FixRatio(-100, 1), FixRatio(75, 1), FixRatio(100, 1),
FixRatio(-75, 1)); /* aim the camera into 3D space */
ViewAngle(FixRatio(30, 1)); /* choose lens focal length */
Identity();
Roll(FixRatio(20, 1));
Pitch(FixRatio(70, 1)); /* roll and pitch the plane */
while (! OSEventAvail(keyOrMouse, &dummy))
{
nBoxes = 0;
do
{
MakeBox();
}
while (nBoxes<boxCount);
PenPat(white);
BackPat(black);
EraseRect(&thePort->portRect);
for ( i = -10; i<=10; i++ )
{
MoveTo3D(FixRatio(i*10, 1), FixRatio(-100, 1), 0);
LineTo3D(FixRatio(i*10, 1), FixRatio(100, 1), 0);
}
for ( i = -10; i<=10; i++ )
{
MoveTo3D(FixRatio(-100, 1), FixRatio(i*10, 1), 0);
LineTo3D(FixRatio(100, 1), FixRatio(i*10, 1), 0);
}
for ( i = nBoxes-1; i>=0; i-- )
{
DrawBrick(boxArray[i].pt1,boxArray[i].pt2);
}
for ( n = 1; n<=1000000; n++ )
{
n = n;
}
}
ShowCursor();
}
DrawBrick(pt1,pt2)
Point3D pt1,pt2;
{
RgnHandle tempRgn;
/* BackColor(Random()); enable for randomly colored boxes */
tempRgn = NewRgn();
OpenRgn();
MoveTo3D(pt1.x, pt1.y, pt1.z); /* front face, y=y1 */
LineTo3D(pt1.x, pt1.y, pt2.z);
LineTo3D(pt2.x, pt1.y, pt2.z);
LineTo3D(pt2.x, pt1.y, pt1.z);
LineTo3D(pt1.x, pt1.y, pt1.z);
CloseRgn(tempRgn);
FillRgn(tempRgn, white);
OpenRgn();
MoveTo3D(pt1.x, pt1.y, pt2.z); /* top face, z=z2 */
LineTo3D(pt1.x, pt2.y, pt2.z);
LineTo3D(pt2.x, pt2.y, pt2.z);
LineTo3D(pt2.x, pt1.y, pt2.z);
LineTo3D(pt1.x, pt1.y, pt2.z);
CloseRgn(tempRgn);
FillRgn(tempRgn, gray);
OpenRgn();
MoveTo3D(pt2.x, pt1.y, pt1.z); /* right face, x=x2 */
LineTo3D(pt2.x, pt1.y, pt2.z);
LineTo3D(pt2.x, pt2.y, pt2.z);
LineTo3D(pt2.x, pt2.y, pt1.z);
LineTo3D(pt2.x, pt1.y, pt1.z);
CloseRgn(tempRgn);
FillRgn(tempRgn, black);
PenPat(white);
MoveTo3D(pt2.x, pt2.y, pt2.z); /* outline right */
LineTo3D(pt2.x, pt2.y, pt1.z);
LineTo3D(pt2.x, pt1.y, pt1.z);
PenNormal();
DisposeRgn(tempRgn);
}
MakeBox()
{
struct Box3D myBox;
int i, j, h, v;
Point3D p1, p2;
Rect myRect;
Rect testRect;
short sw;
p1.x = FixRatio((Random() % 70 - 15), 1);
p1.y = FixRatio((Random() % 70 - 10), 1);
p1.z = 0;
p2.x = p1.x + FixRatio((10 + abs(Random()) % 30), 1);
p2.y = p1.y + FixRatio((10 + abs(Random()) % 45), 1);
p2.z = p1.z + FixRatio((10 + abs(Random()) % 35), 1);
/* reject box if it intersects one already in list */
SetRect(&myRect, HiWord(p1.x), HiWord(p1.y), HiWord(p2.x), HiWord(p2.y));
i=0;
do
{
sw = 0; /* clear exit switch */
SetRect(&testRect, HiWord(boxArray[i].pt1.x), HiWord(boxArray[i].pt1.y),
HiWord(boxArray[i].pt2.x), HiWord(boxArray[i].pt2.y));
InsetRect(&testRect, -1, -1);
if ( SectRect(&myRect, &testRect, &testRect) )
break; /* EXIT(MakeBox); */
sw = 1;
i=i+1;
}
while (i<(nBoxes-1));
if (sw==1) /* sw=1 means previous loop finished */
{
myBox.pt1 = p1;
myBox.pt2 = p2;
/* calc midpoint of box and its distance from the eye */
i = 0;
boxArray[nBoxes].pt1 = myBox.pt1; /* sentinel */
boxArray[nBoxes].pt2 = myBox.pt2;
while (
((myBox.pt1.y > boxArray[i].pt2.y) && (myBox.pt2.y > boxArray[i].pt1.y)) ||
((myBox.pt1.x < boxArray[i].pt2.x) && (myBox.pt2.x < boxArray[i].pt1.x)) )
i = i + 1; /* insert in order of dist */
for ( j = nBoxes; j>=(i+1); j-- )
{
boxArray[j] = boxArray[j - 1];
}
boxArray[i] = myBox;
nBoxes = nBoxes + 1;
}
}